package com.thinvent.remotepassword.util;

import com.thinvent.remotepassword.entity.WikiUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Objects;

public class AuthStorage {
    public static final String TOKEN_KEY = "token";
    private static final HashMap<String, WikiUser> JWT_USER = new HashMap<>();
    public static WikiUser getWikiUser(){
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        return JWT_USER.get(request.getHeader(TOKEN_KEY));
    }
    public static void setUser(String token, WikiUser wikiUser){
        JWT_USER.put(token,wikiUser);
    }
    public static void clearUser(){
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        JWT_USER.remove(request.getHeader(TOKEN_KEY));
    }
}
